#include "Comm.h"
#include "string.h"
 

void Comm::ServerSender(void* threadParams) {
	Sleep(1000);
	portIPSim* args = (portIPSim*)threadParams;
	char Buffer[10];

	cout << "Server Sender Started on: " + args->ipAddress + "Port: " + itoa(args->port,Buffer, 10) << endl;
	try {
		
		SocketServer s = SocketServer(args->port, 10, BlockingSocket);
		//const char* info;	
		Socket* client = s.Accept();

		//SocketClient s = SocketClient(args->ipAddress, args->port);

	
		//get a pointer to the simulation
		Sim* tempSim = args->gameSim;
	
		string temp = "";

		//send orbital bodies
		for(int i = 0; i < tempSim->getNumOrbitalBodies(); i++) {

			GravBody* shit = tempSim->getOrbitalBody(i);
			//send out the initial states of the GravBodies
			//only need to send at beginning, so outside of loop
			//we send the commamd ID, gravBody ID, x pos, y pos, z pos,
			//radius, and mass.			
			temp += Utils::ftoa(INIT_ORBITAL_BODY);	//the command
			temp += ",";
			temp += Utils::ftoa(shit->getID()) ;  //the GravBody's ID
			temp += ",";
			const Vec3f* tloc = shit->getLoc();
			temp += Utils::ftoa(tloc->x);		//the x location
			temp += ",";
			temp += Utils::ftoa(tloc->y);		//the y location
			temp += ",";
			temp += Utils::ftoa(tloc->z);		//the z location
			temp += ",";
			temp += Utils::ftoa(shit->getMass());//the mass
			temp += ",";
			temp += Utils::ftoa(shit->getRad()); //the radius
			temp += ",";
		}
		
		/*cout << temp;
		client->SendLine(temp);
		temp.clear();
		Sleep(1000);*/

		for(int i = 0; i < tempSim->getNumDebrisBodies(); i++) {

			GravBody* shit = tempSim->getDebrisBody(i);
			//send out the initial states of the GravBodies
			//only need to send at beginning, so outside of loop
			//we send the commamd ID, gravBody ID, x pos, y pos, z pos,
			//radius, and mass.	
			temp += Utils::ftoa(INIT_DEBRIS_BODY);	//the command
			temp += ",";
			temp += Utils::ftoa(shit->getID());		//the GravBody's ID
			temp += ",";
			const Vec3f* tloc = shit->getLoc();
			temp += Utils::ftoa(tloc->x);			//the x location
			temp += ",";
			temp += Utils::ftoa(tloc->y);			//the y location
			temp += ",";
			temp += Utils::ftoa(tloc->z);			//the z location
			temp += ",";
			temp += Utils::ftoa(shit->getMass());	//the mass
			temp += ",";
			temp += Utils::ftoa(shit->getRad());		//the radius
			temp += ",";
		}

		//send the init data then wait a bit
		/*cout << temp;
		client->SendLine(temp);
		temp.clear();
		Sleep(1000);*/

		//set the sim as being ready
		temp += Utils::ftoa(CLIENT_SIM_START);
		temp += ",";
		//send out ready signal
		client->SendLine(temp);
		temp.clear();
		Sleep(1000);
		

		while(true) {

			temp = "";

			//Continuously send gravbody data
			//send orbital bodies
			for(int i = 0; i < tempSim->getNumOrbitalBodies(); i++) {

				GravBody* shit = tempSim->getOrbitalBody(i);
				//just need to send the command ID,
				//the GravBody ID, and its x, y, z position				
				temp += Utils::ftoa(UPDATE_ORBITAL_BODY);	//the command
				temp += ",";
				temp += Utils::ftoa(shit->getID()); 	//the GravBody's ID
				temp += ",";
				const Vec3f* tloc = shit->getLoc();
				temp += Utils::ftoa(tloc->x);				//the x location
				temp += ",";
				temp += Utils::ftoa(tloc->y);				//the y location
				temp += ",";
				temp += Utils::ftoa(tloc->z);				//the z location
				temp += ",";
				tloc = shit->getForce();
				temp += Utils::ftoa(tloc->x);				//the x force
				temp += ",";
				temp += Utils::ftoa(tloc->y);				//the y force
				temp += ",";
				temp += Utils::ftoa(tloc->z);				//the z force
				temp += ",";
			}


			//send debris bodies
			for(int i = 0; i < tempSim->getNumDebrisBodies(); i++) {

				GravBody* shit = tempSim->getDebrisBody(i);
				//just need to send the command ID,
				//the GravBody ID, and its x, y, z position				
				temp += Utils::ftoa(UPDATE_DEBRIS_BODY);		//the command
				temp += ",";
				temp += Utils::ftoa(shit->getID());			//the GravBody's ID
				temp += ",";
				const Vec3f* tloc = shit->getLoc();
				temp += Utils::ftoa(tloc->x);				//the x location
				temp += ",";
				temp += Utils::ftoa(tloc->y);				//the y location
				temp += ",";
				temp += Utils::ftoa(tloc->z);				//the z location
				temp += ",";
			}

			//send out location updates then wait a bit
			client->SendLine(temp);
			temp.clear();
			Sleep(50);			
		}
		s.Close();																			
	}
	catch ( string str ) { cerr << "ServerSender was bad: " << str << endl; }
	catch ( ... ) { cerr << "unhandled exception\n"; }
}